<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>0);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_51job_job_group',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级工种'),
			array('op'=>'edit','title'=>'添加一级工种'),
			array('op'=>'listadd','title'=>'批量导入'),
		),
		'list'=>array('data'=>'list','list'=>array(	
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'image','title'=>'图标','type'=>'image'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),				
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_51job/group/setstatus0/','title'=>'一键下架'),
			array('type'=>'post','url'=>'jy_51job/group/setstatus1/','title'=>'一键启用'),
		),'action'=>array(
			array('type'=>'href','op'=>'list2','title'=>'二级工种','show'=>array('pid'=>0)),
		)),
	);
	$return['pids']=pdo_getall('jy_51job_job_group',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='list2'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>0);
	if(!empty($_RQ['id'])){
		$params['pid']=intval($_RQ['id']);
		$return['params']['pid']=intval($_RQ['id']);
	}
	$parenttitle=pdo_getcolumn('jy_51job_job_group',array('id'=>$params['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$parenttitle=$parenttitle?'【'.$parenttitle.'】子':'';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_51job_job_group',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$basic=$this->plugin_setting('basic');
	foreach($return['list'] as &$item){
		$item['is_level3']=$basic['is_level3'];
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级工种'),
			array('op'=>'list2','title'=>$parenttitle.'工种列表'),
			array('op'=>'edit','title'=>'添加工种','query'=>'pid='.$params['pid']),
		),
		'list'=>array('data'=>'list','list'=>array(	
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'pid','title'=>'上级工种','type'=>'select','options'=>'pids'),
			array('name'=>'image','title'=>'图标','type'=>'image'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'switch','set'=>'set'),	
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),				
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_51job/group/setstatus0/','title'=>'一键下架'),
			array('type'=>'post','url'=>'jy_51job/group/setstatus1/','title'=>'一键启用'),
		),'action'=>array(
			array('type'=>'href','op'=>'list3','title'=>'三级工种','show'=>array('is_level3'=>1)),
		)),
	);
	$return['pids']=pdo_getall('jy_51job_job_group',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='list3'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>0);
	if(!empty($_RQ['id'])){
		$params['pid']=intval($_RQ['id']);
		$return['params']['pid']=intval($_RQ['id']);
	}
	$parenttitle=pdo_getcolumn('jy_51job_job_group',array('id'=>$params['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$parenttitle=$parenttitle?'【'.$parenttitle.'】子':'';
	$parentid2=pdo_getcolumn('jy_51job_job_group',array('id'=>$params['pid'],'uniacid'=>$_SESSION['uniacid']),'pid');
	$parenttitle2=pdo_getcolumn('jy_51job_job_group',array('id'=>$parentid2,'uniacid'=>$_SESSION['uniacid']),'title');
	$parenttitle2=$parenttitle2?'【'.$parenttitle2.'】子':'';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_51job_job_group',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$basic=$this->plugin_setting('basic');
	foreach($return['list'] as &$item){
		$item['is_level3']=$basic['is_level3'];
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级工种'),
			array('op'=>'list2','title'=>$parenttitle2.'工种','query'=>'pid='.$parentid2),
			array('op'=>'list3','title'=>$parenttitle.'工种'),
			array('op'=>'edit','title'=>'添加工种','query'=>'pid='.$params['pid']),
		),
		'list'=>array('data'=>'list','list'=>array(	
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'pid','title'=>'上级工种','type'=>'select','options'=>'pids'),
			array('name'=>'image','title'=>'图标','type'=>'image'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'switch','set'=>'set'),		
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),				
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_51job/group/setstatus0/','title'=>'一键下架'),
			array('type'=>'post','url'=>'jy_51job/group/setstatus1/','title'=>'一键启用'),
		)),
	);
	$return['pids']=pdo_getall('jy_51job_job_group',array('pid'=>$parentid2,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_51job_job_group',array('id'=>$id));
		$item['parenttitle']=pdo_getcolumn('jy_51job_job_group',array('id'=>$item['pid']),'title');
	}else{
		$item=array();
		if($_RQ['pid']){
			$item['pid']=intval($_RQ['pid']);
			$item['parenttitle']=pdo_getcolumn('jy_51job_job_group',array('id'=>$item['pid']),'title');
		}
	}
	$item['parenttitle']=$item['parenttitle']?:'无';
    $return['item']=$item;
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'岗位设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回一级工种列表'),
		),
		'edit'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'parenttitle','title'=>'上级工种','type'=>'copy'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'desc','title'=>'描述','type'=>'text'),
			array('name'=>'sn','title'=>'编号','type'=>'string'),
			array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
			array('name'=>'age','title'=>'年龄','type'=>'string'),
			array('name'=>'xueli','title'=>'学历','type'=>'radio','options'=>'xueli_type'),
			array('name'=>'zhengshu','title'=>'资质证书','type'=>'text'),
			array('name'=>'jineng','title'=>'专业技能','type'=>'text'),
			array('name'=>'jinyan','title'=>'相关经验','type'=>'text'),
			array('name'=>'tedian','title'=>'个性特点','type'=>'text'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)
	);
	if($_RQ['pid']){
		array_unshift($htmls['edits'][0]['list'],array('name'=>'parenttitle','title'=>'上级工种','type'=>'string','read'=>true));
	}
	$return['pids']=pdo_getall('jy_51job_job_group',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['xueli_type']=$this->xueli_type;
	$return['sex']=$this->sex;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('sn','title','desc','image','pid','status','paixu','sex','age','xueli','zhengshu','jineng','jinyan','tedian'),$postdata);
	$id=intval($postdata['id']);
	if($id){		
		pdo_update('jy_51job_job_group',$data,array('id'=>$id));
		exi('设置成功！','','goback');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_51job_job_group',$data);
		exi('添加成功！','','goback');
	}
}
if($_RQ['op']=='listadd'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('model')->daoru($postdata);
		exi('导入成功','','/group/list/');
	}
	$return['item']['excelurl']=SITEROOT.'/addons/jy_51job/resource/groupdemo.csv';
	//$return['item']['excelurl']='请直接下载模板选择导入！';
	$htmls=array (
		  'type' => 'edit',
		  'tabs'=>$tabs,
		  'edit' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：在导入过程中，请不要进行任何操作!</red></br>
				功能介绍：可将本模块导出的CSV文件快速上传,节约您的大量时间!</br>
				注意事项：</br>
				1、带*号的为必填项，其余为非必填项。</br>
				2、工种：填写系统中的选项，参考“新业态人才-应聘类型管理-一级工种”。</br>
				3、资质证书要求：证书名称之间，用“,”间隔。</br>
				4、年龄：例：18-60 或不限，不填系统默认为“不限”处理</br>
				5、性别：不填系统默认为“不限”处理</br>
				6、学位/学历：不填系统默认为“不限”处理
				'),
				array ('name' => 'excelurl','type' => 'copy','title' => 'CSV示例文件下载'),
				array ('name' => 'excel','type' => 'upfile','title' => '上传CSV','help'=>'<red>别的表复制内容到新表？如果两个表编码不一样，复制后，会导致新的表编码异常，会导致导入异常。解决办法：先把需被复制的表，统一改成UTF-8编码，然后再复制内容到新表，就行！</red>'),
		  ),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的工种！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_51job_job_group',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_51job_job_group',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='setstatus0'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择工种','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_51job_job_group',array('status'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('下架成功！');
}
if($_RQ['op']=='setstatus1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择工种','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_51job_job_group',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('上架成功！');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_51job_job_group',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_51job_job_group',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>